package com.eclink.iot.rfidclient.mqtt;

import lombok.extern.log4j.Log4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MQTTUtils {

    /**
     * 发送MQTT信息
     * @param topic 主题
     * @param msg 消息
     */
    public static void sendMessage(String topic, String msg) {
        // 临时ClientId
        String clientId = "temp_client_id_" + System.currentTimeMillis();
        String broker = "tcp://127.0.0.1:1883";
        try{
            MqttClient client = new MqttClient(broker, clientId);
            client.connect();
            MqttMessage message = new MqttMessage(msg.getBytes());
            message.setQos(0);
            client.publish(topic, message);
            client.disconnect();
        }catch (Exception ex) {

            ex.printStackTrace();
        }


    }

}
